package com.example.seckillweb.listener;

import com.example.seckillweb.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @description: TODO
 * @author: uu
 * @date: 2023/12/19 12:36
 * @version: 1.0
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = "seckillTopic",
        consumerGroup = "seckill-consumer-group",
        consumeMode = ConsumeMode.CONCURRENTLY,
        consumeThreadNumber = 40)
public class SecKillListener implements RocketMQListener<MessageExt> {
    @Autowired
    GoodsService goodsService;

    @Override
    public void onMessage(MessageExt messageExt) {
        String uk = new String(messageExt.getBody());
        String[] split = uk.split("-");
        Integer goodId = Integer.valueOf(split[0]);
        Integer userId = Integer.valueOf(split[1]);
        // TODO 消息去重
//        System.out.println(userId + ": " + goodId);
//        goodsService.secKill2(userId, goodId);
        log.info("userId={}, goodId={}",userId, goodId);
        goodsService.secKill8(goodId, userId);

    }
}
